home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Examples / PPCExamples / AExamples / AsmSample / asm.s next >
Encoding:
Text File  |  1998-12-03  |  2.7 KB  |  103 lines  |  [TEXT/MPS ]

  1. ; MakeFunction sets up everything you need to make an assembly function 
  2. ; callable from C and debuggable with a symbolic debugger. It does the following:
  3. ; - export the function's transition vector
  4. ; - export the function name
  5. ; - create a toc entry for the function's transition vector
  6. ; - create the transition vector, which must contain
  7. ;     - the function entry point (the name of the function)
  8. ;     - the TOC anchor (the predefined variable TOC[tc0])
  9. ; - tell PPCAsm to create a function entry point symbol for symbolic debuggers
  10. ; - create a csect for the function (one csect per function lets the
  11. ;    linker do dead code stripping, resulting in smaller executables)
  12.     
  13.     MACRO
  14.     MakeFunction &fnName
  15.         EXPORT &fnName[DS]
  16.          EXPORT .&fnName[PR]
  17.         
  18.         TC &fnName[TC], &fnName[DS]
  19.             
  20.         CSECT &fnName[DS]
  21.             DC.L .&fnName[PR]
  22.              DC.L TOC[tc0]
  23.         
  24.         CSECT .&fnName[PR]
  25.         FUNCTION .&fnName[PR]    
  26.         
  27.     ENDM
  28.     
  29. linkageArea:        set 24    ; constant comes from the PowerPC Runtime Architecture Document
  30. CalleesParams:        set    32    ; always leave space for GPR's 3-10
  31. CalleesLocalVars:    set 0    ; ClickHandler doesn't have any
  32. numGPRs:            set 0    ; num volitile GPR's (GPR's 13-31) used by ClickHandler
  33. numFPRs:            set 0    ; num volitile FPR's (FPR's 14-31) used by ClickHandler
  34.  
  35. spaceToSave:    set linkageArea + CalleesParams + CalleesLocalVars + 4*numGPRs + 8*numFPRs  
  36.  
  37.     ; declare the C function DisplayAlert as external
  38.     import .DisplayAlert
  39.     
  40.     import gHelloString        ; global variable from C program
  41.     import gGoodbyeString    ; global variable from C program
  42.  
  43.     toc
  44.         tc gHelloString[TC], gHelloString
  45.         tc gGoodbyeString[TC], gGoodbyeString
  46.     
  47.  
  48. ; Call the MakeFunction macro, defined in MakeFunction.s to begin the function
  49.     MakeFunction    ClickHandler        
  50.     
  51. ; PROLOGUE - called routine's responsibilities
  52.         mflr    r0                    ; Get link register
  53.         stw        r0, 0x0008(SP)        ; Store the link resgister on the stack
  54.         stwu    SP, -spaceToSave(SP); skip over the stack space where the caller        
  55.                                     ; might have saved stuff
  56.         
  57. ; FUNCTION BODY
  58.         extsh    r3,r3
  59.         li      r9,0
  60.         cmpwi    r3,1
  61.         bne        else_if
  62.         lwz        r4,gHelloString[TC](RTOC)
  63.         lwz     r9,0x0000(r4)
  64.         b        end_of_if
  65. else_if:
  66.         cmpwi    cr1,r3,2
  67.         bne        cr1,end_of_if
  68.         lwz        r5,gGoodbyeString[TC](RTOC)
  69.         lwz     r9,0x0000(r5)
  70. end_of_if:
  71.     
  72.         ori     r3,r9,0x0000
  73.         
  74.         ; Now call DisplayAlert.  The parameter is in register 3
  75.         ; No need to save any registers since we don't use them after this call.
  76.  
  77.         bl        .DisplayAlert
  78.         nop                        ; this may be fixed up by the linker
  79.  
  80. ; EPILOGUE - return sequence        
  81.         lwz        r0,0x8+spaceToSave(SP)    ; Get the saved link register
  82.         addic    SP,SP,spaceToSave        ; Reset the stack pointer
  83.         mtlr    r0                        ; Reset the link register
  84.         blr                                ; return via the link register
  85.     
  86.     csect .ClickHandler[pr]
  87.         dc.b 'AsmSample Example - end of assembly code'
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.